package com.obrock;

import android.database.Cursor;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User: Дмитрий Г. Кузнецов
 * Date: 26.09.12
 * Time: 12:29
 * To change this template use File | Settings | File Templates.
 */
public class Complex implements Serializable {

    public static final int TYPE_NORMAL = 0;
    public static final int TYPE_AUTO = 1;

    long complexID = -1;
    double amount = 0.0;
    long parentID = -1;
    int type = TYPE_NORMAL;
    String name = "";
    Account account = null;
    long referenceID = -1;

    private static Database db = null;

    public Complex(Database db, long id) {
        this(db);
        Cursor cr = db.getReadableDatabase().rawQuery("SELECT "
                // 0   1           2           3       4
                + "id, id_balance, id_account, amount, name, type, id_ref "
                + "FROM complex "
                + "WHERE id = ?;",
                new String[]{"" + id});
        if (cr.moveToFirst()) {
            complexID = cr.getLong(0);
            parentID = cr.getLong(1);
            account = Main.context != null ? Main.getAccount(cr.getLong(2)) : new Account(db,cr.getLong(2));
            amount = cr.getDouble(3);
            name = cr.getString(4);
            type = cr.getInt(5);
            referenceID = cr.getLong(6);
        }
        cr.close();
    }

    public Complex(Database db, Balance balance) {
        this(db);
        parentID = balance.balanceID;
        account = balance.accountTo;
        amount = balance.amount;
        name = balance.name;
        //type = balance.type;
    }

    public Complex(Database db) {
        this.db = db;
    }
    
    public void append() {
        db.getWritableDatabase().execSQL("INSERT INTO complex "
                + "(id_balance, id_account, amount, name, type, id_ref) "
                + "VALUES (?,?,?,?,?,?);",
                new Object[]{parentID, account == null ? -1 : account.accountID, amount, name, type, referenceID});
        complexID = db.getIdent("complex");

        DropboxThread.dataChanged();
    }

    public void update() {
        db.getWritableDatabase().execSQL("UPDATE complex SET "
                + "id_balance=?, id_account=?, amount=?, name=?, type=?, id_ref=? "
                + "WHERE id=?;",
                new Object[]{parentID, account == null ? -1 : account.accountID, amount, name, type, referenceID});
        DropboxThread.dataChanged();
    }

    public void delete() {
        db.getWritableDatabase().execSQL("DELETE FROM complex "
                + "WHERE id = ? ",
                new Object[]{complexID});

        if (parentID != -1) {
            Cursor cr = db.getReadableDatabase().rawQuery("SELECT COUNT(*) "
                    + "FROM complex "
                    + "WHERE id_balance = ?",
                    new String[]{"" + parentID});
            if (cr.moveToFirst() && cr.getInt(0) == 0) {
                db.getWritableDatabase().execSQL("UPDATE balance SET "
                        + "type = ? "
                        + "WHERE id = ?;",
                        new Object[]{Balance.TYPE_NORMAL, parentID});
            }
        }
        DropboxThread.dataChanged();
    }
}
